package paper.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import keter.domain.Account;
import keter.domain.validation.BeanValidators;
import keter.service.CodeListService;
import keter.service.UnitService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import paper.domain.User;
import paper.service.UserService;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Controller
@RequestMapping("/paper/user")
public class UserController extends BaseController {
		
	@Autowired
	UserService service;
	
	@Autowired
	UnitService us;
	
	@Autowired
	CodeListService cls;

	private static final Logger logger = LoggerFactory.getLogger(UserController.class);

	public UserController() {
	}
	
	public static class Views {
		private static final String Prefix = "paper/user";
		static final String Index = to_(Prefix) + "index";
		static final String Add = to_(Prefix) + "add";
		static final String Update = to_(Prefix) + "update";
		static final String Delete = to_(Prefix) + "delete";
	}
    
    /**
     * <p>
     * Method ：update
     * <p>
     * Description : 进入修改页面
     * 
     * @param model
     * @param id
     * @return     @Valid Customer customer,BindingResult binder
     * @author 顾力行-gulx@neusoft.com
     */
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update() {
    	List<User> teacherList = Lists.newArrayList();
    	if(getCurrentAccount().getUnit()!=null){
    		teacherList =  service.getDao().findUserByUnitId(getCurrentAccount().getUnit().getParentId());
    	}
        Map<String, List> map = Maps.newHashMap();
        map.put("teacherList", teacherList);
        return Views.Update;
    }
    
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> update(HttpServletRequest request) {
        Map<String, String> ajaxMap = getAjaxMap();
        User user = getUserFromRequest(request);
        if(request.getParameter("teacher")!=null){
	        Long teacherId = Long.valueOf((String)request.getParameter("teacher")); 
	    	user.setTeacher(service.getDao().findOne(teacherId));
        }
        logger.info(user.getEmail());
        Account a = getCurrentAccount();
        if(request.getParameter("unit")!=null){
	    	Long unitId = Long.valueOf((String)request.getParameter("unit")); 
	        a.setUnit(us.getDao().findOne(unitId));
        }
        a.setRepeatPassword(a.getPassword());
        user.setAccount(a);
        BeanValidators.validateWithException(validator, user);
        service.getDao().save(user);
        return ajaxMap;
    }
    
    private User getUserFromRequest(HttpServletRequest request){
    	User user = new User();
    	String id = (String)request.getParameter("id");
    	if(!StringUtils.isEmpty(id)){
    		user.setId(Long.valueOf(id));
    	}
    	user.setName((String)request.getParameter("name"));
    	user.setEmail((String)request.getParameter("email"));
    	user.setDegree((String)request.getParameter("degree"));
    	user.setGraduate((String)request.getParameter("graduate"));
    	user.setPhone((String)request.getParameter("phone"));
    	user.setQQ((String)request.getParameter("QQ"));
    	return user;
    }
    
	@RequestMapping(value="/findteacher/{unitId}", method = RequestMethod.GET)
    @ResponseBody
    public List<User> findTeacher(@PathVariable Long unitId) {
		return service.getDao().findUserByUnitId(unitId);
	}
 
}